home *** CD-ROM | disk | FTP | other *** search
/ Experimental BBS Explossion 3 / Experimental BBS Explossion III.iso / c / cp1.zip / TRANSLAT.C < prev    next >
C/C++ Source or Header  |  1993-04-26  |  2KB  |  78 lines

  1. ===========================================================================
  2.  BBS: The Abacus * HST/DS * Potterville, MI
  3. Date: 04-23-93 (22:53)             Number: 80
  4. From: JERRY COFFIN                 Refer#: NONE
  5.   To: ALL                           Recvd: NO  
  6. Subj: translat.c                     Conf: (36) C Language
  7. ---------------------------------------------------------------------------
  8. /* Public Domain by Jerry Coffin.  Tested with MSC 7.0.  Should
  9.  * be quite portable.
  10.  *
  11.  * Interpets a string in a manner similar to that the compiler
  12.  * does string literals in a program.  All escape sequences are
  13.  * longer than their translated equivalant, so the string is
  14.  * translated in place and either remains the same length or
  15.  * becomes shorter.
  16.  */
  17.  
  18. #include <string.h>
  19. #include <stdio.h>
  20.  
  21. char *translate(char *string ) {
  22.  
  23.     char *here=string;
  24.     size_t len=strlen(string);
  25.     int num;
  26.     int numlen;
  27.  
  28.     while (NULL!=(here=strchr(here,'\\'))) {
  29.         numlen=1;
  30.         switch(here[1]) {
  31.             case '\\':
  32.                 break;
  33.             case 'r':
  34.                 *here = '\r';
  35.                 break;
  36.             case 'n':
  37.                 *here = '\n';
  38.                 break;
  39.             case 't':
  40.                 *here = '\t';
  41.                 break;
  42.             case 'v':
  43.                 *here = '\v';
  44.                 break;
  45.              case 'a':
  46.                 *here = '\a';
  47.                 break;
  48.              case '0':
  49.              case '1':
  50.              case '2':
  51.              case '3':
  52.              case '4':
  53.              case '5':
  54.              case '6':
  55.              case '7':
  56.                 numlen = sscanf(here,"%o",&num);
  57.                 *here = (char)num;
  58.                 break;
  59.              case 'x':
  60.                 numlen = sscanf(here,"%x",&num);
  61.                 *here = (char) num;
  62.                 break;
  63.         }
  64.                 num = here - string + numlen;
  65.         here++;
  66.         memmove(here,here+numlen,len-num );
  67.     }
  68.     return string;
  69. }
  70.     Later,
  71.     Jerry.
  72.  
  73. --- PPoint 1.38
  74.  * Origin: Point Pointedly Pointless (1:128/77.3)
  75. SEEN-BY: 1/211 11/2 4 13/13 101/1 108/89 109/25 110/69 114/5 123/19 124/1
  76. SEEN-BY: 153/752 154/40 77 157/2 159/100 125 575 950 203/23 209/209 280/1
  77. SEEN-BY: 390/1 396/1 5 15 2270/1 2440/5 3603/20
  78.